Imports System.Data
Imports WebControls_Survey

Partial Class WebForms_CInfo_Risk
    Inherits WebFormCommon

    Dim strPath As String

    Private Sub SetPageSecurity()
        Dim sec As New Secure
        Dim cfunc As New CIMSfunc

        sec.ParentPage = parentpage

        sec.SetPageAccess(Session("UserName"))

        If ViewState("mode") = "view" Then
            cfunc.FormDisable(divMain)

            Dim dgi As GridViewRow

            For Each dgi In risk_dg.Rows
                If dgi.RowType = DataControlRowType.DataRow Then
                    Dim s As WebControls_Survey = dgi.Cells(1).FindControl("chDtl")

                    If Not IsNothing(s) Then s.Disable()
                End If
            Next
        End If

        cfunc = Nothing
        sec = Nothing

    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        Select Case PageControlMode
            Case ControlMode.NewBusiness
                strPath = "New_Business"
            Case ControlMode.ClientServicing
                strPath = "Client_Servicing"
        End Select

        If Not Page.IsPostBack Then
            Call InitVars()

            Policy_td.InnerText = ViewState("PolicyNo")
            Provider_td.InnerText = ViewState("Provider")
            Name_td.InnerText = ViewState("CName")

            Call SetPageSecurity()
            Call LoadRiskMstr()

            Dim sec As New Secure

            sec.ParentPage = ParentPage

            If IsNothing(ViewState("CID")) Then
                sec.AddControl(Save_btn, Secure.ControlTypes.AddType)
            Else
                sec.AddControl(Save_btn, Secure.ControlTypes.EditType)
            End If

            sec.SetPageAccess(Session("UserName"))
            sec = Nothing
        End If

        SetPageSecurity()
    End Sub

    Private Sub LoadRiskMstr()
        Dim vo As New DAOMain.RISK_PROFILE_MSTR_VO
        Dim dao As New DAOMain.RISK_PROFILE_MSTR_DAO

        vo.RISK_PROFILE_CD = "%%"
        Dim dt As DataTable = dao.Search(vo, , True)

        If dt.Rows.Count > 0 Then
            risk_dg.DataSource = dt
            risk_dg.DataBind()
        End If

        vo = Nothing
        dao = Nothing
    End Sub

    Protected Sub risk_dg_PageIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles risk_dg.PageIndexChanged
        risk_dg.DataBind()
    End Sub

    Protected Sub risk_dg_PageIndexChanging(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewPageEventArgs) Handles risk_dg.PageIndexChanging
        risk_dg.PageIndex = e.NewPageIndex
        LoadRiskMstr()
    End Sub

    Protected Sub risk_dg_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles risk_dg.RowDataBound

        'Dim ID_lbl As Label = e.Row.Cells(1).FindControl("lblID")
        If e.Row.RowType = DataControlRowType.DataRow Then
            e.Row.Cells(0).ForeColor = e.Row.BackColor

            Dim s As WebControls_Survey = e.Row.Cells(1).FindControl("chDtl")

            'If Not IsNothing(ID_lbl) Then ID_lbl.Text = e.Row.Cells(0).Text
            If Not IsNothing(s) Then
                s.Seq_CD = ViewState("Seq_CD")
                s.Client_ID = ViewState("CID")
                s.Risk_Code = e.Row.DataItem("risk_profile_cd")
                s.DataBind()
            End If
        End If
    End Sub

    Protected Sub Save_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Save_btn.Click
        Dim gvr As GridViewRow
        Dim blnsuccess As Boolean = False

        Try
            For Each gvr In risk_dg.Rows
                If gvr.RowType = DataControlRowType.DataRow Then
                    Dim s As WebControls_Survey = gvr.Cells(1).FindControl("chDtl")

                    If Not IsNothing(s) Then
                        s.Seq_CD = ViewState("Seq_CD")
                        s.Client_ID = ViewState("CID")
                        s.Risk_Code = gvr.Cells(0).Text
                        s.Update()
                    End If
                End If
            Next

            blnsuccess = True
        Catch ex As Exception

        End Try

        Dim cFunc As New PolyFunc.ASPFunc
        If blnsuccess Then
            cFunc.MessageAlert(ParentPage, "Record(s) Saved!")
        Else
            cFunc.MessageAlert(ParentPage, "Error Saving Record(s)!")
        End If
        cFunc = Nothing
    End Sub

    Protected Sub Main_Info_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Main_Info_btn.Click, Cancel_btn.Click
        Call InitSessions()
        Response.Redirect("~/" & strPath & "/Add_CInfo.aspx")
    End Sub

    Protected Sub Other_Info_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Other_Info_btn.Click
        Call InitSessions()
        Response.Redirect("~/" & strPath & "/Add_CInfo_Other.aspx")
    End Sub

    Protected Sub Correspondence_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Correspondence_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/Add_CInfo_Cor.aspx")
    End Sub

    Protected Sub Attachments_btn_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Attachments_btn.Click
        InitSessions()
        Response.Redirect("~/" & strPath & "/Add_CInfo_Att.aspx")
    End Sub
End Class
